package top.yunmouren.areamusic;


import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import top.yunmouren.areamusic.commannd.MainExecutor;
import top.yunmouren.areamusic.commannd.MainTabCompleter;
import top.yunmouren.areamusic.commannd.PlayerSelectionCache;
import top.yunmouren.areamusic.config.ConfigManager;
import top.yunmouren.areamusic.event.*;

import java.io.IOException;
import java.util.Objects;

public class AreaMusic extends JavaPlugin {
    private ConfigManager configManager = null;
    private BukkitTask task;
    private PlayerSelectionCache playerSelectionCache = null;
    public AreaMusic areamusic = null;

    public AreaMusic() {
    }

    public void onEnable() {
        areamusic = this;

        this.playerSelectionCache = new PlayerSelectionCache();
        this.configManager = new ConfigManager(this);

        try {
            this.configManager.load();
        } catch (IOException var2) {
            var2.printStackTrace();
        }
        this.getServer().getPluginManager().registerEvents(new PlayerMoveListener(this), this);
        this.getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
        this.getServer().getPluginManager().registerEvents(new PlayerTeleportListener(this), this);
        this.getServer().getPluginManager().registerEvents(new PlayerChatListener(this), this);
        this.getServer().getPluginManager().registerEvents(new PlayerInteractBlockListener(this), this);
        this.task = Bukkit.getScheduler().runTaskTimer(this, new PeriodCheckRunnable(this), 0L, (long) this.configManager.getCheckPeriod());
        Objects.requireNonNull(this.getServer().getPluginCommand("am")).setExecutor(new MainExecutor(this));
        Objects.requireNonNull(this.getServer().getPluginCommand("am")).setTabCompleter(new MainTabCompleter(this));
    }

    public void onReload() throws IOException {
        new BukkitRunnable() {
            @Override
            public void run() {
                areamusic.playerSelectionCache = new PlayerSelectionCache();
                areamusic.task.cancel();
                try {
                    areamusic.configManager.reload();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                areamusic.task = Bukkit.getScheduler().runTaskTimer(
                        areamusic,
                        new PeriodCheckRunnable(areamusic),
                        0L,
                        (long) areamusic.configManager.getCheckPeriod());
            }
        }.runTaskAsynchronously(areamusic);
    }

    public void onDisable() {
        this.playerSelectionCache = null;
        this.task.cancel();
        try {
            this.configManager.unload();
        } catch (IOException var2) {
            var2.printStackTrace();
        }

    }

    public ConfigManager getConfigManager() {
        return this.configManager;
    }

    public PlayerSelectionCache getPlayerSelectionCache() {
        return this.playerSelectionCache;
    }


}
